package com.zhike.b2b.html;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zhike.b2b.Constants;
import com.zhike.b2b.advert.IAdsService;
import com.zhike.b2b.base.BaseAction;
import com.zhike.b2b.bizservice.impl.IBizServService;
import com.zhike.b2b.buy.IBuyService;
import com.zhike.b2b.company.ICompanyService;
import com.zhike.b2b.expo.IExpoService;
import com.zhike.b2b.job.IJobService;
import com.zhike.b2b.mall.IGoodsService;
import com.zhike.b2b.market.IMarketService;
import com.zhike.b2b.model.AdvertVo;
import com.zhike.b2b.model.BizServVo;
import com.zhike.b2b.model.BuyVo;
import com.zhike.b2b.model.ClassVo;
import com.zhike.b2b.model.CompanyVo;
import com.zhike.b2b.model.ExpoVo;
import com.zhike.b2b.model.JobVo;
import com.zhike.b2b.model.LinkVo;
import com.zhike.b2b.model.MarketVo;
import com.zhike.b2b.model.NewsClassVo;
import com.zhike.b2b.model.NewsVo;
import com.zhike.b2b.model.ProductClassVo;
import com.zhike.b2b.model.ProductVo;
import com.zhike.b2b.news.INewsService;
import com.zhike.b2b.product.IProductService;
import com.zhike.b2b.sysparam.IParamService;
import com.zhike.b2b.util.ClassUtil;
import com.zhike.b2b.util.StringUtil;

@Controller
public class IndexAction extends BaseAction {
	
	@RequestMapping("/admin/createindex.do")
	public String createIndexHtml(HttpServletRequest request,Model model)
	{
		//数据准备
		Map<String,Object> data = new HashMap<String,Object>();
		//生成首页标题
		String webtitle = ps.getParamValue("webtitle");
		data.put("webtitle", webtitle+Constants.SLOGAN);
		data.put("keywords", Constants.KEYWORDS);
		data.put("description", Constants.DESCRIPTION);
		//获取热点资讯页链接
		NewsClassVo ncv = ns.getClassById("100");
		String ncvurl = ClassUtil.getListUrl(ncv,1);
		data.put("newsmore", ncvurl);
		//获取热点资讯
		List<NewsVo> topNews = ns.queryNews("", "0,5", "oper_time");
		dealNewsTitleLength(topNews,12);
		data.put("topNews", topNews);
        //获取产品类别列表
		List<ClassVo> lcv = pcs.queryProductClzs("100", "0,8", true);
		data.put("prodClzs", lcv);
		//获取推荐企业列表
		List<CompanyVo> lcoms = cs.queryCompanies("","add_date","0,20","1");
		dealComsNameLength(lcoms,8);
		data.put("lcoms", lcoms);
		//获取首页展会类别链接
		ClassVo ecv = es.getClassById("100");
		data.put("exporul", ecv.getListUrl());
		//获取展会列表(只显示文字)
		List<ExpoVo> txtLE = es.queryExpos("", "3,8", "oper_time");
		dealExpoNameLength(txtLE,14,5);
		data.put("txtLE", txtLE);
		//获取展会列表(显示图片和标题)
		List<ExpoVo> imgLE = es.queryExpos("", "0,3", "oper_time");
		dealExpoNameLength(imgLE,8,5);
		data.put("imgLE", imgLE);
		//获取推荐产品列表
		List<ProductVo> recomProds100 =prs.queryProducts("100100","1","0,20","oper_time");
		dealProdNameLength(recomProds100,5);
		data.put("recomProds100", recomProds100);
		List<ProductVo> recomProds101 =prs.queryProducts("100101","1","0,20","oper_time");
		dealProdNameLength(recomProds101,5);
		data.put("recomProds101", recomProds101);
		List<ProductVo> recomProds102 =prs.queryProducts("100102","1","0,20","oper_time");
		dealProdNameLength(recomProds102,5);
		data.put("recomProds102", recomProds102);
		List<ProductVo> recomProds103 =prs.queryProducts("100103","1","0,20","oper_time");
		dealProdNameLength(recomProds103,5);
		data.put("recomProds103", recomProds103);
		List<ProductVo> recomProds104 =prs.queryProducts("100104","1","0,20","oper_time");
		dealProdNameLength(recomProds104,5);
		data.put("recomProds104", recomProds104);
		
		//获取首页新闻类型标题
		NewsClassVo inxncv = ns.getClassById("100100");
		data.put("inxncv", inxncv.getClassName());
		//获取本站公告
		List<NewsVo> bullNews = ns.queryNews("100133", "0,4", "oper_time");
		dealNewsTitleLength(bullNews,9);
		data.put("bullNews", bullNews);
		//获取最新动态
		List<NewsVo> latestNews = ns.queryNews("100100", "0,4", "oper_time");
		dealNewsTitleLength(latestNews,9);
		data.put("latestNews", latestNews);
		//获取最新招聘信息
		List<JobVo> newJobs = js.queryJobs("100", "0,4", "oper_time");
		dealJobNameLength(newJobs,9);
		data.put("newJobs", newJobs);
		//获取最新商业服务信息
		List<BizServVo> newServs = bss.queryBizServices("100", "0,4", "oper_time");
		dealServNameLength(newServs,9);
		data.put("newServs", newServs);
		//获取求购类别1
		ClassVo cv1 = is.getClassById("100100");
		data.put("cv1", cv1.getClassName());
		ClassVo cv2 = is.getClassById("100101");
		data.put("cv2", cv2.getClassName());
		ClassVo cv3 = is.getClassById("100102");
		data.put("cv3", cv3.getClassName());
		//获取求购信息
		List<BuyVo> lbvs1 = is.queryBuys("100100", "", "0,6", "oper_time");
		dealBuyNameLength(lbvs1,4);
		data.put("lbvs1", lbvs1);
		List<BuyVo> lbvs2 = is.queryBuys("100101", "", "0,6", "oper_time");
		dealBuyNameLength(lbvs2,4);
		data.put("lbvs2", lbvs2);
		List<BuyVo> lbvs3 = is.queryBuys("100101", "", "0,6", "oper_time");
		dealBuyNameLength(lbvs3,4);
		data.put("lbvs3", lbvs3);
		
		//获取企业列表
		List<CompanyVo> listcoms = cs.queryCompanies("","oper_time","0,12","");
		dealComsNameLength(listcoms,12);
		data.put("listcoms", listcoms);
		//获取商家新闻
		List<NewsVo> comNews = ns.queryNews("100157", "0,12", "oper_time");
		dealNewsTitleLength(comNews,12);
		data.put("comNews", comNews);
		//获取友情链接列表
		List<LinkVo> llv = ls.queryLinks("100000000000000", "30");
		data.put("frilinks", llv);
		//获取底部信息
		hs.getFooterInfo(data);
        //生成页面
		hs.crateHTML(request.getSession().getServletContext(), data, "/template/default/index.ftl", "/index.html");
		model.addAttribute("url","/index.html");
		model.addAttribute("createType", "1");
		return "/admin/html/result";
	}
	
	@RequestMapping("/index/newestsalebuy.do")
	public String newestSaleBuy(Model model)
	{
		List<ProductVo> lpv = prs.getNewestProducts(0,15);
		model.addAttribute("lspv", lpv);
		List<BuyVo> lbv = is.getNewestBuyList(0,15);
		model.addAttribute("lsbv", lbv);
		return "/jsp/newest_sale_buy";
	}
	
	@RequestMapping("/index/classall.do")
	public String getClassAll(Model model)
	{
		Map<String,List<ProductClassVo>> mspcvs = pcs.getClassNameByChar();
		model.addAttribute("mspcvs", mspcvs);
		return "/jsp/all_prod_class";
	}
	
	@RequestMapping("/index/commendprods.do")
	public String getCommendProds(Model model)
	{
		List<ProductVo> cpvs = prs.getRecommendProds(0, 6);
		model.addAttribute("cpvs", cpvs);
		return "/jsp/recommend_prods";
	}
	
	@RequestMapping("/index/indexleft.do")
	public String getIndexLeft(Model model)
	{
		List<ProductVo> spvs = gs.querySpecProds("100102",0, 4);
		model.addAttribute("spvs", spvs);
		List<ProductClassVo> kzlpcvs = gs.getGoodsClass("kongzhi");
		model.addAttribute("kzlpcvs", kzlpcvs);
		List<ProductClassVo> sslpcvs = gs.getGoodsClass("sheshi");
		model.addAttribute("sslpcvs", sslpcvs);
		return "/jsp/index_right";
	}
	
	@RequestMapping("/index/pricelist.do")
	public String getPriceList(Model model)
	{
		List<MarketVo> lmvs = ms.getPriceList();
		model.addAttribute("lmvs", lmvs);
		return "/jsp/price_list";
	}
	@RequestMapping("/index/ad.do")
	public String getAds(@RequestParam("ad_id") String adId,@RequestParam("ad_type")String type,
			@RequestParam("width") int width,@RequestParam("height") int height,Model model)
	{
		AdvertVo avo = as.getAd(adId);
		model.addAttribute("avo", avo);
		model.addAttribute("type", type);
		model.addAttribute("width", width);
		model.addAttribute("height", height);
		return "/jsp/ad";
	}
	
	@RequestMapping("/index/newcoms.do")
	public String getNewComs(Model model)
	{
		List<CompanyVo> lcv = cs.getNewCompanyList(0,12);
		model.addAttribute("lcv", lcv);
		return "/jsp/company_list";
	}
	
	private void dealNewsTitleLength(List<NewsVo> topNews,int length)
	{
		for(NewsVo nv : topNews)
		{
			String title = nv.getTitle();
			nv.setTitle(StringUtil.truncate(title, length));
		}
	}
	
	private void dealComsNameLength(List<CompanyVo> lcoms,int length)
	{
		for(CompanyVo cv : lcoms)
		{
			String title = cv.getCustName();
			cv.setCustName(StringUtil.truncate(title, length));
		}
	}

	private void dealExpoNameLength(List<ExpoVo> les,int nameLength,int venueLength)
	{
		for(ExpoVo cv : les)
		{
			String title = cv.getExpoName();
			cv.setExpoName(StringUtil.truncate(title, nameLength));
			
			String venue = cv.getVenue();
			cv.setVenue(StringUtil.truncate(venue, venueLength));
		}
	}
	//处理产品(供应)名称长度
	private void dealProdNameLength(List<ProductVo> lprods,int length)
	{
		for(ProductVo pv : lprods)
		{
			String title = pv.getProductName();
			pv.setProductName(StringUtil.truncate(title, length));
		}
	}

	//处理招聘信息标题长度
	private void dealJobNameLength(List<JobVo> jobs,int length)
	{
		for(JobVo jv : jobs)
		{
			String title = jv.getJobName();
			jv.setJobName(StringUtil.truncate(title, length));
		}
	}
	//处理商业服务标题长度
	private void dealServNameLength(List<BizServVo> servs,int length)
	{
		for(BizServVo jv : servs)
		{
			String title = jv.getServiceName();
			jv.setServiceName(StringUtil.truncate(title, length));
		}
	}
	//处理求购信息标题长度
	private void dealBuyNameLength(List<BuyVo> buys,int length)
	{
		for(BuyVo jv : buys)
		{
			String title = jv.getBuyName();
			jv.setBuyName(StringUtil.truncate(title, length));
		}
	}
}
